<?php

namespace app\admin\controller;

use think\Db;
use think\Request;
use think\Validate;
use app\facade\Redis;

use app\models\{IntergralBanner, IntergralCate, IntergralGoods, User as Users, Riderlist, IntergralOrder, Region,IntergralOrdergoods};

class Shop extends Base
{

    public function banner()
    {
        $ModelFun = new IntergralBanner();
        $data = $this->request->get();
        $list = $ModelFun->order('id desc')->paginate(20, false, ['query' => $this->request->param()]);
        foreach ($list as $lists) {
            if (!empty($lists['catid'])) {
                $lists['classname'] = (new IntergralCate())->where('id', $lists['catid'])->value('name');
            }
        }
        return view('', ['list' => $list]);
    }

    //添加修改
    public function banner_add()
    {
        if ($this->request->isPost()) {
            $rdslist = Redis::get('clickreturn');
            if ($rdslist) {
                return json(['status' => 1, 'data' => [], 'msg' => '请' . $rdslist . 's后再试']);
            }
            Redis::setex('clickreturn', 2, 1);
            $data = input('post.');
            if (empty($data['id'])) {
                $result = (new IntergralBanner())->allowField(true)->save($data);
            } else {
                $result = (new IntergralBanner())->allowField(true)->save($data, ['id' => $data['id']]);
            }
            if ($result) {
                return json(['status' => 0, 'msg' => '操作成功']);
            } else {
                return json(['status' => 1, 'msg' => '操作失败']);
            }
        } else {
            $id = $this->request->get('id');
            $row = (new IntergralBanner())->where('id', $id)->find();
            $class = (new IntergralCate())->select();
            return view('', ['row' => $row, 'class' => $class]);
        }
    }

    public function banner_del()
    {
        $id = trim(input('id'), ',');
        $result = (new IntergralBanner())->where('id', $id)->delete();
        if ($result) {
            return json(['status' => 0, 'msg' => '操作成功']);
        } else {
            return json(['status' => 1, 'msg' => '删除失败']);
        }
    }


    public function cate()
    {
        $ModelFun = new IntergralCate();
        $data = $this->request->get();
        $list = $ModelFun->order('id desc')->paginate(20, false, ['query' => $this->request->param()]);
        return view('', ['list' => $list]);
    }

    //添加修改
    public function cate_add()
    {
        if ($this->request->isPost()) {
            $rdslist = Redis::get('clickreturn');
            if ($rdslist) {
                return json(['status' => 1, 'data' => [], 'msg' => '请' . $rdslist . 's后再试']);
            }
            Redis::setex('clickreturn', 2, 1);
            $data = input('post.');
            if (empty($data['id'])) {
                $result = (new IntergralCate())->allowField(true)->save($data);
            } else {
                $result = (new IntergralCate())->allowField(true)->save($data, ['id' => $data['id']]);
            }
            if ($result) {
                return json(['status' => 0, 'msg' => '操作成功']);
            } else {
                return json(['status' => 1, 'msg' => '操作失败']);
            }
        } else {
            $id = $this->request->get('id');
            $row = (new IntergralCate())->where('id', $id)->find();
            return view('', ['row' => $row]);
        }
    }

    public function cate_del()
    {
        $id = trim(input('id'), ',');
        $result = (new IntergralCate())->where('id', $id)->delete();
        if ($result) {
            return json(['status' => 0, 'msg' => '操作成功']);
        } else {
            return json(['status' => 1, 'msg' => '删除失败']);
        }
    }


    public function goods()
    {
        $ModelFun = new IntergralGoods();
        $data = $this->request->get();
        $list = $ModelFun->order('id desc');
        if (!empty($data['puton'])) {
            $list = $list->where('puton', $data['puton']);
        }
        if (!empty($data['cateid'])) {
            $list = $list->where('cateid', $data['cateid']);
        }
        if (!empty($data['title'])) {
            $list = $list->where('title', 'like', '%' . $data['title'] . '%');
        }
        $list = $list->paginate(20, false, ['query' => $this->request->param()]);
        foreach ($list as $lists) {
            $lists['images'] = json_decode($lists['images'], true);
            $lists['detail'] = json_decode($lists['detail'], true);
            if (!empty($lists['cateid'])) {
                $lists['classname'] = (new IntergralCate())->where('id', $lists['cateid'])->value('name');
            }
        }
        $class = (new IntergralCate())->select();
        return view('', ['list' => $list, 'class' => $class]);
    }

    //添加修改
    public function goods_add()
    {
        if ($this->request->isPost()) {
            $rdslist = Redis::get('clickreturn');
            if ($rdslist) {
                return json(['status' => 1, 'data' => [], 'msg' => '请' . $rdslist . 's后再试']);
            }
            Redis::setex('clickreturn', 2, 1);
            $data = input('post.');
            if (!empty($data['images'])) {
                $data['images'] = json_encode($data['images'], true);
            }
            if (!empty($data['detail'])) {
                $data['detail'] = json_encode($data['detail'], true);
            }
            if (empty($data['id'])) {
                $result = (new IntergralGoods())->allowField(true)->save($data);
            } else {
                $result = (new IntergralGoods())->allowField(true)->save($data, ['id' => $data['id']]);
            }
            if ($result) {
                return json(['status' => 0, 'msg' => '操作成功']);
            } else {
                return json(['status' => 1, 'msg' => '操作失败']);
            }
        } else {
            $id = $this->request->get('id');
            $row = (new IntergralGoods())->where('id', $id)->find();
            $class = (new IntergralCate())->select();
            if (!empty($row['images'])) {
                $row['images'] = json_decode($row['images'], true);
            }
            if (!empty($row['detail'])) {
                $row['detail'] = json_decode($row['detail'], true);
            }
            return view('', ['row' => $row, 'class' => $class]);
        }
    }

    public function goods_del()
    {
        $id = trim(input('id'), ',');
        $result = (new IntergralGoods())->where('id', $id)->delete();
        if ($result) {
            return json(['status' => 0, 'msg' => '操作成功']);
        } else {
            return json(['status' => 1, 'msg' => '删除失败']);
        }
    }


    public function order()
    {
        $ModelFun = new IntergralOrder();
        $data = $this->request->get();
        $list = $ModelFun->order('id desc');
        if (!empty($data['status'])) {
            $list = $list->where('status', $data['status']);
        }
        if (!empty($data['ordersn'])) {
            $list = $list->where('id', ($data['ordersn'] - pow(21, 8)));
        }
        $list = $list->paginate(20, false, ['query' => $this->request->param()]);
        foreach ($list as $lists) {
            $lists['province_text'] = (new Region())->where(['area_code' => $lists['province'], 'level' => 1])->value('name');
            $lists['city_text'] = (new Region())->where(['area_code' => $lists['city'], 'level' => 2])->value('name');
            $lists['area_text'] = (new Region())->where(['area_code' => $lists['area'], 'level' => 3])->value('name');
            $lists['ordersn'] = $lists['id'] + pow(21, 8);
        }
        return view('', ['list' => $list]);
    }

    public function orderstr()
    {
        $data = input('post.');
        if (empty($data['id']) || empty($data['status'])) {
            return json(['status' => 1, 'msg' => '参数缺失']);
        }
        $order = (new IntergralOrder())->where('id', $data['id'])->find();
        if (empty($order)) {
            return json(['status' => 1, 'msg' => '订单不存在']);
        }
        if ($data['status'] == 2 && ($order['status'] != 1 && $order['status'] != 2)) {
            return json(['status' => 1, 'msg' => '订单状态不可发货操作']);
        }
        if ($data['status'] == 2  && (empty($data['postalname']) || empty($data['postalcode']))) {
            return json(['status' => 1, 'msg' => '物流信息不完整']);
        }
        if ($data['status'] == 3 && $order['status'] != 2) {
            return json(['status' => 1, 'msg' => '订单状态不可完成']);
        }
        if ($data['status'] == 4 && $order['status'] != 1) {
            return json(['status' => 1, 'msg' => '订单状态不可取消']);
        }
        $user = (new Users())->where('id', $order['user_id'])->find();
        Db::startTrans();
        try {
            if($data['status'] == 2){
                (new IntergralOrder())->where(['id' => $data['id'], 'status' => $order['status']])->update(['status' => $data['status'],'postalname' => $data['postalname'],'postalcode' => $data['postalcode']]);
            }else{
                (new IntergralOrder())->where(['id' => $data['id'], 'status' => $order['status']])->update(['status' => $data['status']]);
            }
            if ($data['status'] == 4 && $order['total'] > 0) {
                (new Users())->IntergralEdit($user['id'], $user['integral'], $order['total'], 2, $order['id'], '后台取消');
            }
            Db::commit();
        } catch (\Exception $e) {
            dump($e->getMessage());
            Db::rollback();
            $this->result([], 1, '操作失败');
        }
        $this->result([], 0, '操作成功');
    }

    public function ordergoods(){
        $id = $this->request->get('id');
        $row = (new IntergralOrdergoods())->where('orderid', $id)->find();
        if (!empty($row['images'])) {
            $row['images'] = json_decode($row['images'], true);
        }
        return view('', ['row' => $row]);
    }
}
